/**
 * @file      product_media_osd.c
 * @brief     product media osd source
 *
 * Copyright (c) 2017 Huawei Tech.Co.,Ltd
 *
 * @author    HiMobileCam Reference Develop Team
 * @date      2018/3/17
 * @version   1.0

 */
#include <pthread.h>
#include <sys/prctl.h>
#include "mpi_isp.h"
#include "mpi_awb.h"
#include "hi_mapi_vcap.h"
#include "hi_mapi_vproc.h"
#include "hi_mapi_venc.h"
#include "hi_mapi_disp.h"
#include "hi_product_res.h"
#include "hi_product_media.h"
#include "hi_appcomm_util.h"
#include "product_media_inner.h"

#if defined(AMP_LINUX_HUAWEILITE)
#include "product_media_msg.h"
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */

/** Maximum TimeOSD String Length */
#define PDT_MEDIA_MAX_TIME_OSD_LEN        (19)
/* Maximum InfoOSD String Length */
#define PDT_MEDIA_MAX_INFO_OSD_LEN        (64)

#if defined(CONFIG_ISP_OSD_SUPPORT_ON)
#define ISP_INFO_OSD
/* PQ Debug Info OSD Posotion in percent */
#define OSD_INFO_START_X        (1)
#define OSD_INFO_START_Y        (80)
#define OSD_INFO_FONT_WIDTH     (36)
#define OSD_INFO_FONT_HEIGHT    (72)
#endif

/*define osd base font size on 1080p*/
#define OSD_BASE_FONT_W (48)
#define OSD_BASE_FONT_H (96)
#define OSD_BASE_RES_W (1920)
#define OSD_BASE_RES_H (1080)


typedef struct tagPDT_MEDIA_OSDCtx
{
    HI_BOOL init;
    HI_PDT_MEDIA_OSD_CFG_S osdCfg;
#if defined(ISP_INFO_OSD)
    HI_BOOL infoTskRun[HI_PDT_MEDIA_VCAP_DEV_MAX_CNT];
    pthread_t infoTskId[HI_PDT_MEDIA_VCAP_DEV_MAX_CNT];
    HI_MAPI_OSD_ATTR_S infoOsdAttr[HI_PDT_MEDIA_VCAP_DEV_MAX_CNT];
#endif
} PDT_MEDIA_OSDCtx;
static PDT_MEDIA_OSDCtx g_mediaOsdCtx;

#if (defined(AMP_LINUX_HUAWEILITE) && defined(__HuaweiLite__)) || defined(AMP_HUAWEILITE) || defined(AMP_LINUX)
/**
 * @brief   get logo bitmap information
 * @param[in]u32ResHeight : venc resoluton height
 * @param[out]pstBitmap : logo bitmap
 * @return 0 success
 * @return -1 failure, invalid resolution height
 */
static HI_S32 PDT_MEDIA_LoadLogoBmp(BITMAP_S* bitmap)
{
#define LOGO_SIZE_L           (160)
#define LOGO_SIZE_S           (86)

    HI_APPCOMM_CHECK_POINTER(bitmap, HI_FAILURE);
    HI_PDT_RES_DATA data;
    HI_U8* osdLogo = NULL;

    if (bitmap->u32Width >= LOGO_SIZE_L) {
        HI_PDT_RES_GetData(HI_PDT_RES_PURP_OSDLOGO_MAIN, &data);
    } else if (bitmap->u32Width >= LOGO_SIZE_S) {
        HI_PDT_RES_GetData(HI_PDT_RES_PURP_OSDLOGO_SUB, &data);
    } else {
        MLOGE("Invalid Res Height[%ux%u]\n", bitmap->u32Width,bitmap->u32Height);
        return HI_FAILURE;
    }
    osdLogo = data.memAddr;
    HI_APPCOMM_CHECK_POINTER(osdLogo, HI_FAILURE);

    /* set logo osd pixel format */
    bitmap->enPixelFormat = HI_MAPI_OSD_PIXEL_FMT_RGB1555;

    memcpy(&(bitmap->enPixelFormat), osdLogo, sizeof(PIXEL_FORMAT_E));
    osdLogo += sizeof(PIXEL_FORMAT_E);

    memcpy(&(bitmap->u32Width), osdLogo, sizeof(HI_U32));
    osdLogo += sizeof(HI_U32);

    memcpy(&(bitmap->u32Height), osdLogo, sizeof(HI_U32));
    osdLogo += sizeof(HI_U32);

    bitmap->pData = (HI_VOID*)osdLogo;
    return HI_SUCCESS;
}
#endif

static HI_S32 PDT_MEDIA_GetVcapImageSize(HI_MAPI_OSD_DISP_ATTR_S* osdDispAttr, SIZE_S* imageSize)
{
    HI_S32 ret = HI_SUCCESS;
    HI_HANDLE vcapDevHdl = osdDispAttr->u32Batch/10;
    HI_MAPI_VCAP_ATTR_S vcapAttr = {0};
    ret = HI_MAPI_VCAP_GetAttr(vcapDevHdl, &vcapAttr);
    if (ret != HI_SUCCESS) {
        MLOGE("HI_MAPI_VCAP_GetAttr(%d) fail,Error Code: [0x%08X]\n", vcapDevHdl, ret);
        return ret;
    }
    memcpy(imageSize, &(vcapAttr.astVcapPipeAttr[osdDispAttr->ModHdl].astPipeChnAttr[osdDispAttr->ChnHdl].stDestSize),
        sizeof(SIZE_S));
    return ret;
}

static HI_S32 PDT_MEDIA_GetVprocImageSize(HI_MAPI_OSD_DISP_ATTR_S* osdDispAttr, SIZE_S* imageSize)
{
    HI_S32 ret = HI_SUCCESS;
    HI_MAPI_VPORT_ATTR_S vportAttr = {0};
    ret = HI_MAPI_VPROC_GetPortAttr(osdDispAttr->ModHdl, osdDispAttr->ChnHdl, &vportAttr);
    if (ret != HI_SUCCESS) {
        MLOGE("HI_MAPI_VPROC_GetPortAttr(%d,%d) fail,Error Code: [0x%08X]\n", osdDispAttr->ModHdl, osdDispAttr->ChnHdl, ret);
        return ret;
    }
    imageSize->u32Width = vportAttr.u32Width;
    imageSize->u32Height = vportAttr.u32Height;
    return ret;
}

static HI_S32 PDT_MEDIA_GetVencImageSize(HI_MAPI_OSD_DISP_ATTR_S* osdDispAttr, SIZE_S* imageSize)
{
    HI_S32 ret = HI_SUCCESS;
    HI_MAPI_VENC_ATTR_S vencAttr = {0};
    ret = HI_MAPI_VENC_GetAttr(osdDispAttr->ChnHdl, &vencAttr);
    if (ret != HI_SUCCESS) {
        MLOGE("HI_MPI_VENC_GetChnAttr(%d) fail,Error Code: [0x%08X]\n", osdDispAttr->ChnHdl, ret);
        return ret;
    }
    imageSize->u32Width = vencAttr.stVencPloadTypeAttr.u32Width;
    imageSize->u32Height = vencAttr.stVencPloadTypeAttr.u32Height;
    return ret;
}

static HI_S32 PDT_MEDIA_GetDispImageSize(HI_MAPI_OSD_DISP_ATTR_S* osdDispAttr, SIZE_S* imageSize)
{
    HI_S32 ret = HI_SUCCESS;
    HI_MAPI_DISP_WINDOW_ATTR_S wndAttr = {0};
    ret = HI_MAPI_DISP_GetWindowAttr(osdDispAttr->ModHdl, osdDispAttr->ChnHdl, &wndAttr);
    if (ret != HI_SUCCESS) {
        MLOGE("HI_MAPI_DISP_GetWindowAttr(%d,%d) fail,Error Code: [0x%08X]\n", osdDispAttr->ModHdl, osdDispAttr->ChnHdl, ret);
        return ret;
    }
    imageSize->u32Width = wndAttr.stRect.u32Width;
    imageSize->u32Height = wndAttr.stRect.u32Height;
    return ret;
}


static HI_S32 PDT_MEDIA_AdaptFontSize(SIZE_S baseFontSize,SIZE_S baseImageSize,HI_MAPI_OSD_ATTR_S* osdAttr)
{
    HI_S32 ret = HI_SUCCESS;
    SIZE_S imageSize = {0};
    if (baseFontSize.u32Width==0 || baseFontSize.u32Height==0) {
        baseFontSize.u32Width = OSD_BASE_FONT_W;
        baseFontSize.u32Height= OSD_BASE_FONT_H;
    }
    if (baseImageSize.u32Width==0 || baseImageSize.u32Height==0) {
        baseImageSize.u32Width = OSD_BASE_RES_W;
        baseImageSize.u32Height= OSD_BASE_RES_H;
    }
    HI_MAPI_OSD_DISP_ATTR_S* dispAttr = &osdAttr->astDispAttr[0];
    switch (dispAttr->enBindedMod) {
        case HI_MAPI_OSD_BINDMOD_VCAP: {
            ret = PDT_MEDIA_GetVcapImageSize(dispAttr, &imageSize);
            if (ret != HI_SUCCESS) {
                MLOGE("PDT_MEDIA_GetVcapImageSize(%d,%d) fail,Error Code: [0x%08X]\n", dispAttr->ModHdl, dispAttr->ChnHdl, ret);
                return ret;
            }
            break;
        }
        case HI_MAPI_OSD_BINDMOD_VPROC: {
            ret = PDT_MEDIA_GetVprocImageSize(dispAttr, &imageSize);
            if (ret != HI_SUCCESS) {
                MLOGE("PDT_MEDIA_GetVprocImageSize(%d,%d) fail,Error Code: [0x%08X]\n", dispAttr->ModHdl, dispAttr->ChnHdl, ret);
                return ret;
            }
            break;
        }
        case HI_MAPI_OSD_BINDMOD_VENC: {
            ret = PDT_MEDIA_GetVencImageSize(dispAttr, &imageSize);
            if (ret != HI_SUCCESS) {
                MLOGE("PDT_MEDIA_GetVencImageSize(%d) fail,Error Code: [0x%08X]\n", dispAttr->ChnHdl, ret);
                return ret;
            }
            break;
        }
        case HI_MAPI_OSD_BINDMOD_DISP: {
            ret = PDT_MEDIA_GetDispImageSize(dispAttr, &imageSize);
            if (ret != HI_SUCCESS) {
                MLOGE("PDT_MEDIA_GetDispImageSize(%d,%d) fail,Error Code: [0x%08X]\n", dispAttr->ModHdl, dispAttr->ChnHdl, ret);
                return ret;
            }
            break;
        }
        default:
            MLOGW("invalide BindedMod [%d]\n", dispAttr->enBindedMod);
            return HI_EINVAL;
    }
    if (osdAttr->stContent.enType == HI_MAPI_OSD_TYPE_TIME) {
        osdAttr->stContent.stTimeContent.stFontSize.u32Width = imageSize.u32Width*baseFontSize.u32Width/baseImageSize.u32Width;
        osdAttr->stContent.stTimeContent.stFontSize.u32Width = HI_APPCOMM_ALIGN(osdAttr->stContent.stTimeContent.stFontSize.u32Width, 2);
        osdAttr->stContent.stTimeContent.stFontSize.u32Height = imageSize.u32Height*baseFontSize.u32Height/baseImageSize.u32Height;
        osdAttr->stContent.stTimeContent.stFontSize.u32Height = HI_APPCOMM_ALIGN(osdAttr->stContent.stTimeContent.stFontSize.u32Height, 2);
    } else if (osdAttr->stContent.enType == HI_MAPI_OSD_TYPE_STRING){
        osdAttr->stContent.stStrContent.stFontSize.u32Width = imageSize.u32Width*baseFontSize.u32Width/baseImageSize.u32Width;
        osdAttr->stContent.stStrContent.stFontSize.u32Width = HI_APPCOMM_ALIGN(osdAttr->stContent.stStrContent.stFontSize.u32Width, 2);
        osdAttr->stContent.stStrContent.stFontSize.u32Height = imageSize.u32Height*baseFontSize.u32Height/baseImageSize.u32Height;
        osdAttr->stContent.stStrContent.stFontSize.u32Height = HI_APPCOMM_ALIGN(osdAttr->stContent.stStrContent.stFontSize.u32Height, 2);
    } else {
        MLOGW("Osd type [%d] is no need to do fonts adapt.\n", osdAttr->stContent.enType);
    }
    return HI_SUCCESS;
}

#if defined(ISP_INFO_OSD)
#if (defined(AMP_LINUX_HUAWEILITE) && defined(__HuaweiLite__)) || defined(AMP_HUAWEILITE) || defined(AMP_LINUX)
static HI_VOID PDT_MEDIA_OSD_GetInfoStr(VI_PIPE viPipe, HI_CHAR* str, HI_U32 strLen)
{
    ISP_EXP_INFO_S expInfo;
    ISP_WB_INFO_S wbInfo;
    ISP_INNER_STATE_INFO_S innerStateInfo;
    memset(&expInfo,0,sizeof(ISP_EXP_INFO_S));
    memset(&wbInfo,0,sizeof(ISP_WB_INFO_S));
    memset(&innerStateInfo,0,sizeof(ISP_INNER_STATE_INFO_S));
    HI_MPI_ISP_QueryExposureInfo(viPipe, &expInfo);
    HI_MPI_ISP_QueryInnerStateInfo(viPipe, &innerStateInfo);
    HI_MPI_ISP_QueryWBInfo(viPipe, &wbInfo);

    snprintf(str, strLen, "S:%u L:%u Ag:%u IspDg:%u R:%u CT:%u",
             expInfo.u32ExpTime, expInfo.u32ShortExpTime, expInfo.u32AGain, expInfo.u32ISPDGain,
             innerStateInfo.u32WDRExpRatioActual[0], wbInfo.u16ColorTemp);
    MLOGD("ViPipe[%d], DebugStr[%s]\n", viPipe, str);
}

static HI_VOID* PDT_MEDIA_OSD_UpdateInfoTsk(HI_VOID* param)
{
    prctl(PR_SET_NAME, "OSDInfoTsk", 0, 0, 0);
    HI_S32 ret = HI_SUCCESS;
    HI_PDT_MEDIA_CFG_S* mediaCfg = PDT_MEDIA_GetMediaCfg();
    HI_PDT_MEDIA_OSD_CFG_S* osdCfg = &g_mediaOsdCtx.osdCfg;
    HI_S32 devIndex = *(HI_S32*)param;
    VI_PIPE viPipe =  mediaCfg->stVideoCfg.stVcapCfg.astVcapDevAttr[devIndex].astVcapPipeAttr[0].VcapPipeHdl;
    HI_S32 osdIdx = osdCfg->s32OsdCnt + devIndex;
    HI_MAPI_OSD_ATTR_S * infoOsdAttr = &g_mediaOsdCtx.infoOsdAttr[devIndex];

    PDT_MEDIA_OSD_GetInfoStr(viPipe, infoOsdAttr->stContent.stStrContent.szStr, PDT_MEDIA_MAX_INFO_OSD_LEN);
    ret = HI_MAPI_OSD_SetAttr(osdIdx, infoOsdAttr);
    if (ret == HI_SUCCESS) {
        ret = HI_MAPI_OSD_Start(osdIdx);
        HI_APPCOMM_CHECK_RETURN(ret, NULL);
    } else {
        if (ret != HI_MAPI_OSD_ENOFONT) {
            return NULL;
        }
        MLOGW("OsdIdx[%d]: No Osdfont.\n",osdIdx);
    }

    while (g_mediaOsdCtx.infoTskRun[devIndex]) {
        PDT_MEDIA_OSD_GetInfoStr(viPipe, infoOsdAttr->stContent.stStrContent.szStr, PDT_MEDIA_MAX_INFO_OSD_LEN);
        ret = HI_MAPI_OSD_SetAttr(osdIdx, infoOsdAttr);
        if (ret != HI_SUCCESS) {
            MLOGE("HI_OSD_SetAttr[%d] failed %x\n", osdIdx,ret);
            break;
        }
        HI_usleep(1*1000*1000);
    }

    HI_MAPI_OSD_Stop(osdIdx);
    return NULL;
}
#endif
#endif

HI_S32 PDT_MEDIA_StartInfoOsd(HI_VOID)
{
#if defined(ISP_INFO_OSD)
#if (defined(AMP_LINUX_HUAWEILITE) && defined(__HuaweiLite__)) || defined(AMP_HUAWEILITE) || defined(AMP_LINUX)
    HI_S32 ret = HI_SUCCESS;
    HI_PDT_MEDIA_OSD_CFG_S* osdCfg = &g_mediaOsdCtx.osdCfg;
    HI_PDT_MEDIA_CFG_S* mediaCfg = PDT_MEDIA_GetMediaCfg();
    static HI_S32 vcapDevIdx[HI_PDT_MEDIA_VCAP_DEV_MAX_CNT];
    HI_S32 i = 0;
    HI_S32 osdIdx = 0;
    for (i = 0; i < HI_PDT_MEDIA_VCAP_DEV_MAX_CNT ; ++i) {
        vcapDevIdx[i] = i;
        if (mediaCfg->stVideoCfg.stVcapCfg.astVcapDevAttr[i].astVcapPipeAttr[0].bIspBypass) {
            continue;
        }
        /* Use before TimeOsd attr to fill InfoOsd Attr*/
        HI_MAPI_OSD_ATTR_S * infoOsdAttr = &g_mediaOsdCtx.infoOsdAttr[i];
        for (osdIdx = osdCfg->s32OsdCnt-1; osdIdx >= 0; osdIdx--) {
            if (osdCfg->astOsdAttr[osdIdx].stContent.enType == HI_MAPI_OSD_TYPE_TIME) {
                HI_S32 dispIdx = 0;
                for (dispIdx = 0; dispIdx < osdCfg->astOsdAttr[osdIdx].u32DispNum; ++dispIdx) {
                    if (osdCfg->astOsdAttr[osdIdx].astDispAttr[dispIdx].u32Batch== vcapDevIdx[i] * 10 + HI_MAPI_OSD_TYPE_TIME) {
                        infoOsdAttr->astDispAttr[0] = osdCfg->astOsdAttr[osdIdx].astDispAttr[dispIdx];
                        infoOsdAttr->stContent.u32Color = osdCfg->astOsdAttr[osdIdx].stContent.u32Color;
                        infoOsdAttr->stContent.stStrContent.u32BgColor = osdCfg->astOsdAttr[osdIdx].stContent.stStrContent.u32BgColor;
                        break;
                    }
                }
            }
        }
        infoOsdAttr->u32DispNum = 1;
        infoOsdAttr->stContent.enType = HI_MAPI_OSD_TYPE_STRING;
        infoOsdAttr->astDispAttr[0].enCoordinate = HI_MAPI_OSD_COORDINATE_RATIO_COOR;
        infoOsdAttr->astDispAttr[0].stStartPos.s32X = OSD_INFO_START_X;
        infoOsdAttr->astDispAttr[0].stStartPos.s32Y = OSD_INFO_START_Y;
        SIZE_S baseFontSize;
        baseFontSize.u32Width = OSD_INFO_FONT_WIDTH;
        baseFontSize.u32Height= OSD_INFO_FONT_HEIGHT;
        PDT_MEDIA_AdaptFontSize(baseFontSize, osdCfg->stBaseImageSize, infoOsdAttr);
        if (infoOsdAttr->astDispAttr[0].bShow && g_mediaOsdCtx.infoTskId[i] == 0) {
            g_mediaOsdCtx.infoTskRun[i] = HI_TRUE;
            ret = pthread_create(&g_mediaOsdCtx.infoTskId[i], NULL, PDT_MEDIA_OSD_UpdateInfoTsk, &vcapDevIdx[i]);
            if (ret != HI_SUCCESS) {
                g_mediaOsdCtx.infoTskRun[i] = HI_FALSE;
                MLOGE( "s32VcapDevIdx[%d] create OSD_UpdateInfoThread failed %x\n", vcapDevIdx[i],ret);
                return ret;
            }
        }
    }
#else
    HI_MSG_SendASync(HI_MSG_MEDIA_INFO_OSD_START, NULL, 0, NULL);
#endif
#endif
    return HI_SUCCESS;
}

HI_S32 PDT_MEDIA_StopInfoOsd(HI_VOID)
{
#if defined(ISP_INFO_OSD)
#if (defined(AMP_LINUX_HUAWEILITE) && defined(__HuaweiLite__)) || defined(AMP_HUAWEILITE) || defined(AMP_LINUX)
    HI_S32 i = 0;
    for (i = 0; i < HI_PDT_MEDIA_VCAP_DEV_MAX_CNT ; ++i) {
        g_mediaOsdCtx.infoTskRun[i] = HI_FALSE;
        if(g_mediaOsdCtx.infoTskId[i] != 0) {
            pthread_join(g_mediaOsdCtx.infoTskId[i], NULL);
            g_mediaOsdCtx.infoTskId[i]=0;
        }
    }
#else
    HI_MSG_SendSync(HI_MSG_MEDIA_INFO_OSD_STOP, NULL, 0, NULL, NULL, 0);
#endif
#endif
    return HI_SUCCESS;
}

HI_S32 PDT_MEDIA_InitOsd(const HI_PDT_MEDIA_OSD_CFG_S* pstOsdCfg)/* osd init and Set osd bitmap size */
{
    HI_S32 ret = HI_SUCCESS;

    if (g_mediaOsdCtx.init) {
        MLOGI("has already init\n");
        return HI_SUCCESS;
    }
#if !(defined(AMP_LINUX_HUAWEILITE) && defined(__LINUX__))
    /* Init OSD */
    extern HI_S32 PDT_MEDIA_InitOsdFontLib(HI_VOID);
    ret = PDT_MEDIA_InitOsdFontLib();
    HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
#endif
    memset(&g_mediaOsdCtx,0,sizeof(PDT_MEDIA_OSDCtx));
    memcpy(&g_mediaOsdCtx.osdCfg,pstOsdCfg,sizeof(HI_PDT_MEDIA_OSD_CFG_S));
    g_mediaOsdCtx.init = HI_TRUE;
    return ret;
}

/* free osd bitmap size */
HI_S32 PDT_MEDIA_DeinitOsd(HI_VOID)
{
    if (!g_mediaOsdCtx.init) {
        MLOGI("has already deinit\n");
        return HI_SUCCESS;
    }

    HI_S32 ret = HI_SUCCESS;
    ret = HI_MAPI_OSD_Deinit();
    HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);

    g_mediaOsdCtx.init = HI_FALSE;
    return HI_SUCCESS;
}

HI_S32 PDT_MEDIA_StartOsd(const HI_PDT_MEDIA_OSD_CFG_S* pstOsdCfg)
{
   // return HI_SUCCESS;//add hugh
    HI_APPCOMM_CHECK_EXPR(g_mediaOsdCtx.init, HI_PDT_MEDIA_ENOTINIT);
    HI_S32 ret = HI_SUCCESS;
    HI_S32 osdIdx = 0;
    HI_MAPI_OSD_ATTR_S osdAttr = {0};

    for (osdIdx = 0; osdIdx < pstOsdCfg->s32OsdCnt; ++osdIdx) {
        MLOGD("s32OsdIdx=%d\n",osdIdx);
        if (pstOsdCfg->astOsdAttr[osdIdx].u32DispNum == 0) {
            MLOGW("OsdIdx[%d] content disp num is zero.This content do nothing.\n",osdIdx);
            continue;
        }

        if (pstOsdCfg->astOsdAttr[osdIdx].stContent.enType == HI_MAPI_OSD_TYPE_BITMAP) {
#if (defined(AMP_LINUX_HUAWEILITE) && defined(__HuaweiLite__)) || defined(AMP_HUAWEILITE) || defined(AMP_LINUX)
            PDT_MEDIA_LoadLogoBmp((BITMAP_S*)&pstOsdCfg->astOsdAttr[osdIdx].stContent.stBitmapContent);
#else
            ret = HI_MAPI_OSD_GetAttr(osdIdx, &osdAttr);
            HI_APPCOMM_CHECK_RETURN(ret, ret);
            ((HI_PDT_MEDIA_OSD_CFG_S*)pstOsdCfg)->astOsdAttr[osdIdx].stContent.stBitmapContent = osdAttr.stContent.stBitmapContent;
#endif
        }

        if (pstOsdCfg->astOsdAttr[osdIdx].stContent.enType == HI_MAPI_OSD_TYPE_TIME
            ||pstOsdCfg->astOsdAttr[osdIdx].stContent.enType == HI_MAPI_OSD_TYPE_STRING) {
            ret = PDT_MEDIA_AdaptFontSize(pstOsdCfg->stBaseFontSize,pstOsdCfg->stBaseImageSize,(HI_MAPI_OSD_ATTR_S*)&pstOsdCfg->astOsdAttr[osdIdx]);
            if (ret!= HI_SUCCESS) {
                MLOGW("PDT_MEDIA_AdaptFontSize for OSD[%d] fail,skip start this osd\n",osdIdx);
                continue;
            }
        }
        memcpy(&osdAttr, &pstOsdCfg->astOsdAttr[osdIdx], sizeof(HI_MAPI_OSD_ATTR_S));

        ret = HI_MAPI_OSD_SetAttr(osdIdx, &osdAttr);
        if (ret == HI_SUCCESS) {
            ret = HI_MAPI_OSD_Start(osdIdx);
#ifndef CONFIG_USB_HOST_UVC_SUPPORT
            HI_APPCOMM_CHECK_RETURN(ret, ret);
#endif
        } else {
            if (ret != HI_MAPI_OSD_ENOFONT) {
                return ret;
            }
            MLOGW("OsdIdx[%d]: No Osdfont.\n",osdIdx);
        }
    }
    memcpy(&g_mediaOsdCtx.osdCfg,pstOsdCfg,sizeof(HI_PDT_MEDIA_OSD_CFG_S));

#if defined(ISP_INFO_OSD)
    if (pstOsdCfg->s32OsdCnt > 0) {
        PDT_MEDIA_StartInfoOsd();
    }
#endif
    return HI_SUCCESS;
}

HI_S32 PDT_MEDIA_StopOsd(HI_VOID)
{
    HI_APPCOMM_CHECK_EXPR(g_mediaOsdCtx.init, HI_PDT_MEDIA_ENOTINIT);
    HI_S32 ret = HI_SUCCESS;
    HI_PDT_MEDIA_OSD_CFG_S* osdCfg = &g_mediaOsdCtx.osdCfg;
    HI_S32 osdIdx = 0;
    for (osdIdx = 0; osdIdx < osdCfg->s32OsdCnt ; ++osdIdx) {
        MLOGD("s32OsdIdx=%d\n",osdIdx);
        ret = HI_MAPI_OSD_Stop(osdIdx);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }
#if defined(ISP_INFO_OSD)
    PDT_MEDIA_StopInfoOsd();
#endif
    return HI_SUCCESS;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */

